<template>
	<view class="orderList">
		<tui-navigation-bar   :titleSize="18" :titleLineHeight="18" :titlefontWeight="800" :isOpacity="false"
			@init="initNavigation" title="订单管理" backgroundColor="#fff" color="#000">
			<view class="tui-header-icon" :style="{ marginTop: top + 'px' }">
				<tui-icon name="arrowleft" color="#333" @click="back"></tui-icon>
			</view>
		</tui-navigation-bar> 
		 
		  <view :style="{ marginTop: marginTp*2 + 'px' }">
			<z-paging
				:empty-view-img-style="{'display':'none'}"
				empty-view-text="呜~您还没有任何订单..." 
				class="paging" 
				ref="pagingordr"   
				@query="queryList"
				v-model="dataList" 
			>  
			 <template v-slot:top>
				 <view class="header_tab_order"  :style="{ marginTop: marginTp + 'px' }">
				 	<tutor-nav-tab ref="navTabs" :indexTab="Number(indexTab)" :clickColor="tabColor" :textColor="textColor"   :tabTitle="list" @changeTab="changeTab"></tutor-nav-tab>
				 </view>
			 </template>
					 <OrderItem @orderClick="orderClick" :ossUrl="ossUrl" :list="dataList"></OrderItem> 
			</z-paging>
		</view>
		 <tutor-renew-popup :place="place" @clickRenew="clickRenew" :visible="isVisible" @alterVisible="alterVisible"></tutor-renew-popup>
	</view>
</template>

<script>
	import OrderItem from './compontents/order_item.vue' 
	import { getOrderList,getPayRenew,getPay,cancelOrder,delOrder,getPrice} from "@/http/api/order/order.js"
	export default {
		data() {
			return {
				top: 0, //标题图标距离顶部距离
				marginTp: 0, 
				currentTab: 0,
				isVisible:false,
				tabIndex: 0,
				place:"",
				tabColor: '#e45071',
				orderId:'',
				indexTab:0,
				ossUrl:getApp().globalData.ossurl,//图片前缀
				textColor: '#000',
				list: [
					{
						text: '全部订单'
					},
					{
						text: '邀约中'
					},
					{
						text: '已确认'
					},
					{
						text: '待开始'
					},
					{
						text: '服务中'
					},
					{
						text: '待评价'
					},
					{
						text: '已完成'
					},
					// {
					// 	text: '已关闭'
					// },
					// {
					// 	text: '拒绝'
					// }
				],
				dataList: [],
			}
		}, 
		onLoad(options) {
			if(options.status){
				this.indexTab=options.status
				this.currentTab = options.status
				this.$refs.pagingordr && this.$refs.pagingordr.reload()
			} 
			uni.$on('refOrderList',(e)=>{
				 if(e){
					this.$refs.pagingordr.reload()
				 }
			})  
		},
		computed: { 
			scrollHeight() {
				return 'height:1000rpx';
			}
		},
		components:{
			OrderItem
		},
		methods: {  
			initNavigation(e) {
				this.top = e.top;
				this.marginTp = e.height
			},
			//续约
			alterVisible(value){
				this.isVisible=value
			},
			//确认续约
			clickRenew(item){
				let that=this
				getPayRenew({
					orderId:that.orderId,
					hourNumber:item
				}).then(res=>{
					if(res.code===200){ 
						getApp().globalData.initJssdk(weixin=>{ 
							weixin.chooseWXPay({
								appId: res.data.appId,
								timestamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.packageValue,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success: function (res) {
									// 支付成功后的回调函数
									that.$refs.pagingordr.reload()
								},
								cancel: function (res) {
										// 支付取消回调函数
										console.log(res)
									},
									// 支付失败回调函数
								fail: function (res) {
										console.log(res) 
									}
								});
						})
					}
				})
			},
			
			back() {
				uni.hideLoading();
				uni.navigateBack();
			},
			closeDrawer() {
				this.visible = false
			},
			changeTab(index) { 
				this.currentTab = index;
				this.tabIndex = index;
				this.$refs.pagingordr && this.$refs.pagingordr.reload()
			},
			queryList(pageNo, pageSize) {
				uni.showLoading({
					title: '加载中...'
				}); 
				this.getOrderList(pageNo,pageSize);
			},
			//订单列表
	 	    getOrderList(pageNo = 1,pageSize) { 
				getOrderList({
					status:this.currentTab,
					pageSize:pageSize,
					pageNum:pageNo
				}).then(res=>{
					if(res.code===200){
						let list=res.rows
						this.$refs.pagingordr.complete(list); 
					} 
				})
				.catch(res => {
					// 如果请求失败写this.$refs.paging.complete(false);
					// 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
					// 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
					this.$refs.pagingordr.complete(false);
				})
				.finally(()=>{
					uni.hideLoading();
			}) 
			},
		  //获取支付参数
		  getPayParms(id){
			  let that=this
			  getPay({orderId:id}).then(res=>{
				  if(res.code===200){
					getApp().globalData.initJssdk(weixin=>{ 
							weixin.chooseWXPay({
								appId: res.data.appId,
								timestamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.packageValue,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success: function (res) {
									// 支付成功后的回调函数
									that.$refs.pagingordr.reload()
								},
								cancel: function (res) {
										// 支付取消回调函数
										console.log(res)
									},
									// 支付失败回调函数
								fail: function (res) {
										console.log(res,'时报') 
									}
								});
						})
				  }
			  })
		  },
		   //操作
		   orderClick(data){
			    let type=data.type  
				let that=this
				that.orderId=data.id
			   	switch (type) {
					case '支付': {
						that.getPayParms(data.id)
						console.log("支付");
						break;
					}
					case "取消订单": { 
						uni.showModal({
							title: '提示',
							content: '确认取消该订单吗？',
							showCancel: true,
							cancelColor: "#555",
							confirmColor: "#eb0909",
							confirmText: "确定",
							success(res) {
								if (res.confirm) {
									 cancelOrder(data.id).then(res=>{
									 	if(res.code===200){
											that.$utils.toast('取消成功')
											that.$refs.pagingordr.reload()
										}
									 })
								}
							}
						})
						console.log("取消订单");
						break;
					}
					case "一件续费": {
					 
						getPrice(data.id).then(res=>{
							if(res.code===200){
								that.isVisible=true 
								that.place=res.data.rates
							} 
						})
						// this.place
						console.log("一件续费",data);
						break;
					}
					case "联系教练": {
						console.log("联系教练");
						if(!data.assistant.phoneNumber){
							that.$utils.toast('教练电话不能为空')
							return
						}
						uni.makePhoneCall({
								phoneNumber: data.assistant.phoneNumber, // 替换为实际要拨打的电话号码
								success(res) {
								  console.log('拨打电话成功', res);
								},
								fail(err) {
								  console.error('拨打电话失败', err);
								}
							});
						break;
					}
					case "删除订单": {
						console.log("删除订单");
						uni.showModal({
							title: '提示',
							content: '确认取消该订单吗？',
							showCancel: true,
							cancelColor: "#555",
							confirmColor: "#eb0909",
							confirmText: "确定",
							success(res) {
								if (res.confirm) {
									 delOrder(data.id).then(res=>{
									 	if(res.code===200){
											that.$utils.toast('删除成功')
											that.$refs.pagingordr.reload()
										}
									 })
								}
							}
						})
						break;
					}
					case "立即评价": {
						uni.navigateTo({
							url:'/pages/mine/modules/pushEvaluate/pushEvaluate?orderId='+data.id
						})
						break;
					}
					default: {
						 
						break;
					}
				} 
		   }
		}
	}
</script>

<style lang="scss" scoped>
	@import './orderList.scss';
</style>